import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'
import store from '@/store'
import {message, Modal, notification} from 'ant-design-vue' /// es/notification
import {VueAxios} from './axios'
import {ACCESS_TOKEN} from '@/store/mutation-types'

const vm = new Vue()
let baseURL = '/api'

if (process.env.NODE_ENV === 'production') {
  baseURL = process.env.VUE_APP_API_BASE_URL
}


// 创建 axios 实例
const service = axios.create({
  // --------------修改apiTest---------------------
  baseURL: baseURL,
  timeout: 120000 // 请求超时时间
})

console.log('能不能打印vm的url', process.env)

const err = (error) => {
  if (error.response) {
    const data = error.response.data
    const token = Vue.ls.get(ACCESS_TOKEN)
    
    if (error.response.status === 403) {
      console.log('服务器403啦，要重新登录！')
      notification.error({
        message: 'Forbidden',
        description: data.message
      })
    }
    if (error.response.status === 500) {
      if (data.message.length > 0) {
        message.error(data.message)
      }
    }
    if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
      notification.error({
        message: 'Unauthorized',
        description: 'Authorization verification failed'
      })
      if (token) {
        store.dispatch('Logout').then(() => {
          setTimeout(() => {
            window.location.reload()
          }, 1500)
        })
      }
    }
  }
  return Promise.reject(error)
}

// request interceptor
service.interceptors.request.use(config => {
  console.log('@@@', config)
  const token = Vue.ls.get(ACCESS_TOKEN)
  if (token) {
    config.headers['Authorization'] = 'Bearer ' + token
  }
  if (config.method === 'get') {
    config.paramsSerializer = function (params) {
      return qs.stringify(params, {arrayFormat: 'repeat'})
    }
  }
	if(config.parameter){
		let url = config.url + '?'
		for (const propName of Object.keys(config.parameter)) {
			const value = config.parameter[propName]
			var part = encodeURIComponent(propName) + '='
			if (value !== null && typeof value !== 'undefined') {
				if (typeof value === 'object') {
					for (const key of Object.keys(value)) {
						const params = propName + '[' + key + ']'
						var subPart = encodeURIComponent(params) + '='
						url += subPart + encodeURIComponent(value[key]) + '&'
					}
				} else {
					url += part + encodeURIComponent(value) + '&'
				}
			}
		}
		url = url.slice(0, -1)
		config.parameter = {}
		config.url = url
	}
  return config
}, err)

/**
 * response interceptor
 * 所有请求统一返回
 */
service.interceptors.response.use((response) => {
  
  if (response.request.responseType === 'blob') {
    return response
  }
  const code = response.data.code
  if (code === 1011006 || code === 1011007 || code === 1011008 || code === 1011009) {
    Modal.error({
      title: '提示：',
      content: response.data.message,
      okText: '重新登录',
      onOk: () => {
        Vue.ls.remove(ACCESS_TOKEN)
        window.location.reload()
      }
    })
  } else if (code === 1013002 || code === 1016002 || code === 1015002 || code === 500) {
    message.error(response.data.message)
    return response.data
  } else {
    return response.data
  }
}, err)

const installer = {
  vm: {},
  install(Vue) {
    Vue.use(VueAxios, service)
  }
}

export {
  installer as VueAxios,
  service as axios
}
